package com.wootion.dizhi.comon;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wootion.dizhi.dao.DeviceMapper;
import com.wootion.dizhi.entity.Device;
import com.wootion.dizhi.runData.GlobalData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;

@Component
public class MyRunner implements ApplicationRunner {

    @Resource
    DeviceMapper deviceMapper;

    @Value("${my.deviceNo}")
    private String deviceNo;

    private String resources = "./data/";

    private String runPath = "./run/";

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 减去10分钟
        LocalDateTime tenMinutesBefore = now.minusMinutes(30);
        // LocalDateTime 转 Date
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = tenMinutesBefore.atZone(zoneId);
        Date date = Date.from(zdt.toInstant());


//        GlobalData.DEVICE_STATUS.put( "1",date);
//        GlobalData.DEVICE_STATUS.put( "2",date);
//        GlobalData.DEVICE_STATUS.put( "3",date);
//        GlobalData.DEVICE_STATUS.put( "4",date);

        GlobalData.DEVICE_IS_RUN.put( "1",false);
        GlobalData.DEVICE_IS_RUN.put( "2",false);
        GlobalData.DEVICE_IS_RUN.put( "3",false);
        GlobalData.DEVICE_IS_RUN.put( "4",false);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",3);//未记录
        GlobalData.RUN_DEVICE_DATA.put("1",jsonObject);
        GlobalData.RUN_DEVICE_DATA.put("2",jsonObject);
        GlobalData.RUN_DEVICE_DATA.put("3",jsonObject);
        GlobalData.RUN_DEVICE_DATA.put("4",jsonObject);

        File file2 = new File(resources);
        if (!file2.exists()){
            file2.mkdir();
        }

        File file3 = new File("./out/");
        if (!file3.exists()){
            file3.mkdir();
        }
        File run = new File(runPath);
        if (!run.exists()){
            run.mkdir();
        }
        for (int i = 1; i < 9; i++) {
            if (!Optional.ofNullable(deviceMapper.selectById(String.valueOf(i))).isPresent()){
                Device device = new Device();
                device.setId(UUID.randomUUID().toString());
                device.setDeviceName(String.valueOf(i));
                device.setAddress(String.valueOf(i));
                deviceMapper.insertDevice(device);
            }
        }

        File file1 = new File(resources+"type.MN");
        if (!file1.exists()){
            file1.createNewFile();
            // 将Map转换为字符串
            String jsonString = "{}";
            FileWriter writer = new FileWriter(file1, false);
            writer.write(jsonString);
            writer.close();
        }
        System.out.println("初始化设备信息");
    }
}
